package com.zhf.server.thread;

import com.zhf.model.*;
import com.zhf.server.*;
import com.zhf.server.impl.ExamQuestionServiceImpl;
import com.zhf.server.impl.ExamServiceImpl;
import com.zhf.server.impl.TrainServiceImpl;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class NewClientTrhead extends Thread {
    private ServerInitClose sic;
    private ServerDao sd;
    private ExamQuestionService examQuestionService;
    private TraineeService traineeService;
    private ExamService examService;
    public NewClientTrhead(){

    }

    public NewClientTrhead(ServerInitClose sic, ServerDao sd){
        this.sic = sic;
        this.sd = sd;
    }

    @Override
    public void run() {
        try {
            examQuestionService = new ExamQuestionServiceImpl(sd);
            traineeService = new TrainServiceImpl(sd);
            examService = new ExamServiceImpl(sd);
            currentThread1: while (true){
                ApiRoute apiRoute =(ApiRoute) sic.getOis().readObject();
                switch (apiRoute){
                    case ADD_TRAINEE:
                        System.out.println("调用的的接口地址是：" + ApiRoute.ADD_TRAINEE.getApiRoute());
                        UserMessage userMessageRequest =(UserMessage)sic.getOis().readObject();
                        System.out.println("接收的参数是：" + userMessageRequest);
                        UserMessage userMessageResponse = traineeService.addTrainee(userMessageRequest);
                        System.out.println("返回的结果是：" + userMessageResponse);
                        sic.getOos().writeObject(userMessageResponse);
                        break;
                    case DELETE_TRAINEE:
                        System.out.println("调用的的接口地址是：" + ApiRoute.DELETE_TRAINEE.getApiRoute());
                        User user1 = (User) sic.getOis().readObject();
                        System.out.println("接收的参数是：" + user1);
                        UserMessage userMessage1 = traineeService.deleteTrainee(user1);
                        System.out.println("返回的结果是：" + userMessage1);
                        sic.getOos().writeObject(userMessage1);
                        break;
                    case SELECT_TRAINEE:
                        System.out.println("调用的的接口地址是：" + ApiRoute.SELECT_TRAINEE.getApiRoute());
                        User user = (User)sic.getOis().readObject();
                        System.out.println("接收的参数是：" + user);
                        UserMessage userMessage = traineeService.selectTrainee(user);
                        System.out.println("返回的结果是：" + userMessage);
                        sic.getOos().writeObject(userMessage);
                        break;
                    case UPDATE_TRAINEE:
                        System.out.println("调用的的接口地址是：" + ApiRoute.UPDATE_TRAINEE.getApiRoute());
                        UserUpdVo userUpdVo = (UserUpdVo) sic.getOis().readObject();
                        System.out.println("接收的参数是：" + userUpdVo);
                        UserUpdVo userUpdVo1 = traineeService.updateTrainee(userUpdVo);
                        System.out.println("返回的结果是：" + userUpdVo1);
                        sic.getOos().writeObject(userUpdVo1);
                        break;
                    case ITERATOR_TRAINEE:
                        System.out.println("调用的的接口地址是：" + ApiRoute.ITERATOR_TRAINEE.getApiRoute());
                        List<User> userList = traineeService.listUser();
                        List<User> collect = userList.stream().map(user2 -> {
                            User user3 = new User();
                            user3.setUserName(user2.getUserName());
                            user3.setPassword(user2.getPassword());
                            return user3;
                        }).collect(Collectors.toList());
                        System.out.println("返回的结果是：" + collect);
                        sic.getOos().writeObject(collect);
                        break;

                    case ADD_QUESTION:
                        System.out.println("调用的的接口地址是：" + ApiRoute.ADD_QUESTION.getApiRoute());
                        Question question = (Question) sic.getOis().readObject();
                        System.out.println("接收的参数是：" + question);
                        QuestionMessage questionMessage = examQuestionService.addExamQuestion(question);
                        System.out.println("返回的结果是：" + questionMessage);
                        sic.getOos().writeObject(questionMessage);
                        break;
                    case DELETE_QUESTION:
                        System.out.println("调用的的接口地址是：" + ApiRoute.DELETE_QUESTION.getApiRoute());
                        Question question1 = (Question) sic.getOis().readObject();
                        System.out.println("接收的参数是：" + question1);
                        QuestionMessage questionMessage1 = examQuestionService.deleteExamQuestion(question1);
                        System.out.println("返回的结果是：" + questionMessage1);
                        sic.getOos().writeObject(questionMessage1);
                        break;
                    case UPDATE_QUESTION:
                        System.out.println("调用的的接口地址是：" + ApiRoute.UPDATE_QUESTION.getApiRoute());
                        QuestionUpdVo questionUpdVo = (QuestionUpdVo) sic.getOis().readObject();
                        System.out.println("接收的参数是：" + questionUpdVo);
                        QuestionUpdVo questionUpdVo1 = examQuestionService.updateQuestion(questionUpdVo);
                        System.out.println("返回的结果是：" + questionUpdVo1);
                        sic.getOos().writeObject(questionUpdVo1);
                        break;
                    case SELECT_QUESTION:
                        System.out.println("调用的的接口地址是：" + ApiRoute.SELECT_QUESTION.getApiRoute());
                        Question question2 = (Question) sic.getOis().readObject();
                        System.out.println("接收的参数是：" + question2);
                        QuestionMessage questionMessage2 = examQuestionService.selectQuestion(question2);
                        System.out.println("返回的结果是：" + questionMessage2);
                        sic.getOos().writeObject(questionMessage2);
                        break;
                    case IMPORT_QUESTION:
                        System.out.println("调用的的接口地址是：" + ApiRoute.IMPORT_QUESTION.getApiRoute());
                        List<Question> questions = (List<Question>)sic.getOis().readObject();
                        System.out.println("接收的参数是：" + questions);
                        QuestionImportVo questionImportVo = examQuestionService.importQuestion(questions);
                        System.out.println("返回的结果是：" + questionImportVo);
                        sic.getOos().writeObject(questionImportVo);
                        break;
                    case UPDATE_PASSWORD:
                        System.out.println("调用的的接口地址是：" + ApiRoute.UPDATE_PASSWORD.getApiRoute());
                        UserPassword userPassword = (UserPassword) sic.getOis().readObject();
                        System.out.println("接收的参数是：" + userPassword);
                        UserPasswordMessage userPasswordMessage = traineeService.updatePassword(userPassword);
                        System.out.println("返回的结果是：" + userPasswordMessage);
                        sic.getOos().writeObject(userPasswordMessage);
                        break;
                    case START_EXAMINATION:
                        System.out.println("调用的的接口地址是：" + ApiRoute.START_EXAMINATION.getApiRoute());
                        ExamUser examUser = (ExamUser)sic.getOis().readObject();
                        System.out.println("接收的参数是：" + examUser);
                        ExamUserMessage examUserMessage = examService.startExamination(examUser);
                        System.out.println("返回的结果是：" + examUserMessage);
                        sic.getOos().writeObject(examUserMessage);
                        break;
                    case STATISTIC_EXAM_RESULT:
                        System.out.println("调用的的接口地址是：" + ApiRoute.STATISTIC_EXAM_RESULT.getApiRoute());
                        ExamUser examUser1 = (ExamUser)sic.getOis().readObject();
                        System.out.println("接收的参数是：" + examUser1);
                        ExamUserMessage examUserMessage1 = examService.staticExamResult(examUser1);
                        System.out.println("返回的结果是：" + examUserMessage1);
                        sic.getOos().writeObject(examUserMessage1);
                        break;
                    case QUERY_EXAM_GRADE:
                        System.out.println("调用的的接口地址是：" + ApiRoute.QUERY_EXAM_GRADE.getApiRoute());
                        ExamUser examUser2 = (ExamUser) sic.getOis().readObject();
                        System.out.println("接收的参数是：" + examUser2);
                        ExamUserMessage examUserMessage2 = examService.selectExamUserByExamNameAndUser(examUser2);
                        System.out.println("返回的结果是：" + examUserMessage2);
                        sic.getOos().writeObject(examUserMessage2);
                        break;
                    case EXPORT_EXAM_GRADE:
                        System.out.println("调用的的接口地址是：" + ApiRoute.EXPORT_EXAM_GRADE.getApiRoute());
                        User user2 = (User) sic.getOis().readObject();
                        System.out.println("接收的参数是：" + user2);
                        ExamUserListMessage examUserListMessage = examService.selectExamUserListByUser(user2);
                        System.out.println("返回的结果是：" + examUserListMessage);
                        sic.getOos().writeObject(examUserListMessage);
                        break;
                    case EXIT_TRAINEE:
                        break currentThread1;
                    default:
                        String msg = "服务器端接收消息出错！";
                        System.out.println(msg);
                }
            }
        }
        catch (java.net.SocketException e){
            System.out.println("客户端中断连接！");
        }
        catch (java.io.EOFException e){

        }
        catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            // 关闭资源

        }
    }

    public void closeResource(){

    }
}
